using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
public class Spawner : MonoBehaviour {
	
	public GameObject Building1;
	public GameObject Building2;
	public GameObject Building3;
	public GameObject Building4;
	
	public Transform healthPrefab;
	public Transform ammoPrefab;
	public Transform speedPrefab;
	public Transform humanPrefab;
	public Transform alienPrefab;
    public string poolName = "Creator";
    public int spawnAmount = 50;
    public float spawnInterval = 0.25f;
	
	private Vector3[] PlayerSpawnPoints = new Vector3[4];
	public Transform SpawnPoints;
    private SpawnPool pool;
	
	void OnEnable()
	{
		Messenger<string>.AddListener("heart", SpawnHeart);
		Messenger<string>.AddListener("ammo", SpawnAmmo);
		Messenger<string>.AddListener("speed", SpawnSpeed);
		Messenger<string>.AddListener("alien", SpawnAlien);
		Messenger<string>.AddListener("human", SpawnHuman);
	}
	
	void OnDisable()
	{
		Messenger<string>.RemoveListener("heart", SpawnHeart);
		Messenger<string>.RemoveListener("ammo", SpawnAmmo);
		Messenger<string>.RemoveListener("speed", SpawnSpeed);
		Messenger<string>.RemoveListener("alien", SpawnAlien);
		Messenger<string>.RemoveListener("human", SpawnHuman);
	}
	// Use this for initialization
	void Start () 
	{
		for(int i = 0; i < 4; ++i)
		{
			PlayerSpawnPoints[i] = SpawnPoints.FindChild("Player"+i+"SpawnPoint").position;
			
		}
		
		
		
	}
	
	public void SpawnPrefab(string prefabName, string spawnLocation)
	{
		Vector3 v_location = Vector3.zero;
		switch(spawnLocation)
		{
		case "1":
		{
			v_location = new Vector3(2F + Building1.transform.position.x, 5F, -1F + Building1.transform.position.z);
			break;
		}
		case "2":
		{
			v_location = new Vector3(2F + Building2.transform.position.x, 5F, -1F + Building2.transform.position.z);
			break;
		}
		case "3":
		{
			v_location = new Vector3(2F + Building3.transform.position.x, 5F, -1F + Building3.transform.position.z);
			break;
		}
		case "4":
		{
			v_location = new Vector3(3F + Building4.transform.position.x, 5F, -1F + Building4.transform.position.z);
			break;
		}
		case "Player1":
		{
			Debug.Log("Player 1");
				v_location = PlayerSpawnPoints[0];
				break;
		}
		case "Player2":
		{
				v_location = PlayerSpawnPoints[1];
				break;
		}
		case "Player3":
		{
				v_location = PlayerSpawnPoints[2];
				break;
		}
		case "Player4":
		{
				v_location = PlayerSpawnPoints[3];
				break;
		}
		default:
			v_location = new Vector3(transform.position.x, 5F, transform.position.z);
			break;
		}
		//spawnLocation = new Vector3(Building1.transform.position.x + , 5F, Building1.transform.position.z + );
		Transform prefab;
		switch(prefabName)
		{
		case "health":
		{
			prefab = PoolManager.Pools["Powerups"].Spawn(healthPrefab);
			prefab.position = v_location;
			break;
		}
		case "ammo":
		{
			prefab = PoolManager.Pools["Powerups"].Spawn(ammoPrefab);
			prefab.position = v_location;
			break;
		}
		case "speed":
		{
			prefab = PoolManager.Pools["Powerups"].Spawn(speedPrefab);
			prefab.position = v_location;
			break;
		}
		case "human":
		{
			//Debug.Log(spawnLocation + "UI");
			prefab = PoolManager.Pools["Players"].Spawn(humanPrefab, v_location, Quaternion.identity);
			prefab.name = spawnLocation;
			int num = Int32.Parse(spawnLocation[spawnLocation.Length-1].ToString());
			//Debug.Log(GameObject.Find("UIElements").transform.FindChild(spawnLocation + "UI"));
			prefab.GetComponent<PlayerScript>().SetUI("Player" + num);// = GameObject.Find("UIElements").transform.FindChild(spawnLocation + "UI");
			prefab.GetComponent<PlatformInputController>().playerNum = num;
			//prefab.position = v_location;
			break;
		}
		case "alien":
		{
			prefab = PoolManager.Pools["Players"].Spawn(alienPrefab, v_location, Quaternion.identity);
			prefab.name = spawnLocation;
			int num = Int32.Parse(spawnLocation[spawnLocation.Length-1].ToString());
			
			prefab.GetComponent<PlatformInputController>().playerNum = num;//Int32.Parse(spawnLocation[spawnLocation.Length-1].ToString());
			prefab.GetComponent<PlayerScript>().SetUI("Player" + num);
			//prefab.position = v_location;
			break;
		}
		}		
	}
	void SpawnHeart(string _location)
	{
		SpawnPrefab("health", _location );
	}
	void SpawnAmmo(string _location)
	{
		SpawnPrefab("ammo", _location );
	}
	void SpawnHuman(string _location)
	{
		Debug.Log("Spwan Human called");
		SpawnPrefab("human", _location);
	}
	void SpawnAlien(string _location)
	{
		SpawnPrefab("alien", _location);
	}
	void SpawnSpeed(string _location)
	{
		SpawnPrefab("speed", _location);
	}
	public void DespawnPrefab(Transform xform)
	{
		
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}
